package top.asteam.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.asteam.exception.BusinessException;
import top.asteam.service.IPaymentService;
import top.asteam.entity.Payment;
import top.asteam.entity.CommonResult;

import java.util.List;
import java.util.UUID;

/**
 * @Author Rock Wang
 * @Time 2021/4/10
 */
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private IPaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/getById")
    public CommonResult getById(@RequestParam Long id) {
        Payment byId = paymentService.getById(id);
        return CommonResult.OkData(byId);
    }

    @PostMapping("/save")
    public CommonResult create(@RequestBody @Validated Payment payment) {
        boolean save = paymentService.save(payment);
        if (save) {
            return CommonResult.Ok().setMessage("insert ok , port: "+serverPort);
        } else {
            throw new BusinessException(101, "插入失败");
        }
    }

    @GetMapping("/getAll")
    public CommonResult getAll() {
        List<Payment> list = paymentService.list();
        return CommonResult.OkData(list).setMessage("找到啦！");
    }

    @GetMapping(value = "/zk")
    public CommonResult paymentZK(){
        return CommonResult.Ok().setMessage("springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString());
    }

}
